import Vue from "vue";
import VueRouter from "vue-router";

import Login from "../views/home/login.vue";

import OrganizationUser from "../views/user/organizationUser.vue";
import IndividualUser from "../views/user/individualUser.vue";

import CoachTrainManager from "../views/train/coachTrainManager.vue";
import CoachTrainForm from "../views/train/coachTrainForm.vue";
import RefereeTrainManager from "../views/train/refereeTrainManager.vue";
import RefereeTrainForm from "../views/train/refereeTrainForm.vue";

import CoachCertificationList from "../views/coach/certificationList.vue";
import CoachCertificationDetail from "../views/coach/certificationDetail.vue";
import CoachAnswerList from "../views/coach/coachAnswerList.vue";

import RefereeCertificationList from "../views/referee/certificationList.vue";
import RefereeCertificationDetail from "../views/referee/certificationDetail.vue";
import RefereeExamList from "../views/referee/examList.vue";
import RefereeExamForm from "../views/referee/examForm.vue";

import CoachTrainOrder from "../views/order/coachTrainOrder.vue";
import RefereeTrainOrder from "../views/order/refereeTrainOrder.vue";
import RefereeRegisterOrder from "../views/order/refereeRegisterOrder.vue";
import OrderDetail from "../views/order/orderDetail.vue";
import signlist from "../views/sign/siginlist.vue";
import signForm from "../views/sign/signForm.vue";
import addchfc from "../views/sign/addchfc.vue";
import whitelist from "../views/sign/whitelist.vue";

import group from "../views/member/group.vue";
import groupFom from "../views/member/groupForm.vue";
import thatperson from "../views/member/thatperson.vue";
Vue.use(VueRouter);

const routes = [
  // home
  {
    path: "/",
    name: "Home",
    redirect: "/user/individualUser",
  }, // 首页
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  }, // 登录
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  // 用户管理
  {
    path: "/user/organizationUser",
    name: "OrganizationUser",
    component: OrganizationUser,
    meta: {
      title: "后台用户",
    },
  }, // 后台用户管理
  {
    path: "/user/individualUser",
    name: "IndividualUser",
    component: IndividualUser,
    meta: {
      title: "个人用户",
    },
  }, // 个人用户管理
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  // 培训管理
  {
    path: "/train/coachTrain",
    name: "CoachTrainManager",
    component: CoachTrainManager,
    meta: {
      title: "教练员培训",
    },
  }, // 教练员------培训
  {
    path: "/train/coachTrain/from",
    name: "CoachTrainForm",
    component: CoachTrainForm,
    meta: {
      title: "教练员培训管理",
    },
  }, // 教练员------培训新增/编辑
  {
    path: "/train/refereeTrain",
    name: "RefereeTrainManager",
    component: RefereeTrainManager,
    meta: {
      title: "裁判员培训",
    },
  }, // 裁判员------培训
  {
    path: "/train/refereeTrain/from",
    name: "RefereeTrainForm",
    component: RefereeTrainForm,
    meta: {
      title: "裁判员培训管理",
    },
  }, // 裁判员------培训新增/编辑
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  // 教练员
  {
    path: "/coach/certification",
    name: "CoachCertificationList",
    component: CoachCertificationList,
    meta: {
      title: "注册审核",
    },
  },
  // 教练员------注册审核
  {
    path: "/coach/certificationDetail",
    name: "CoachCertificationDetail",
    component: CoachCertificationDetail,
    meta: {
      title: "注册详情",
    },
  },
  {
    path: "/coach/answerList",
    name: "CoachAnswerList",
    component: CoachAnswerList,
    meta: {
      title: "认证答题记录",
    },
  },
  // 教练员------注册详情
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  // 裁判员
  {
    path: "/referee/certification",
    name: "RefereeCertificationList",
    component: RefereeCertificationList,
    meta: {
      title: "注册审核",
    },
  }, // 裁判员------注册审核
  {
    path: "/referee/certificationDetail",
    name: "RefereeCertificationDetail",
    component: RefereeCertificationDetail,
    meta: {
      title: "注册详情",
    },
  }, // 裁判员------注册详情
  {
    path: "/referee/examList",
    name: "RefereeExamList",
    component: RefereeExamList,
    meta: {
      title: "认证答题",
    },
  }, // 裁判员------认证答题
  {
    path: "/referee/examForm",
    name: "RefereeExamForm",
    component: RefereeExamForm,
    meta: {
      title: "答题管理",
    },
  }, // 裁判员------答题新增/编辑
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  // 订单管理
  {
    path: "/order/coachTrain",
    name: "CoachTrainOrder",
    component: CoachTrainOrder,
    meta: {
      title: "教练员培训",
    },
  }, // 教练员------培训订单
  {
    path: "/order/refereeTrain",
    name: "RefereeTrainOrder",
    component: RefereeTrainOrder,
    meta: {
      title: "裁判员培训",
    },
  }, // 裁判员------培训订单
  {
    path: "/order/refereeRegister",
    name: "RefereeRegisterOrder",
    component: RefereeRegisterOrder,
    meta: {
      title: "裁判员注册",
    },
  }, // 裁判员------注册订单
  {
    path: "/order/orderDetail",
    name: "OrderDetail",
    component: OrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 订单详情

  //签到
  {
    path: "/sign/signlist",
    name: "signlist",
    component: signlist,
    meta: {
      title: "签到活动列表",
    },
  },
  {
    path: "/sign/addchfc",
    name: "addchfc",
    component: addchfc,
    meta: {
      title: "新增签到活动",
    },
  },
  {
    path: "/sign/whitelist",
    name: "whitelist",
    component: whitelist,
    meta: {
      title: "手机号白名单列表",
    },
  },
  {
    path: "/sign/signForm",
    name: "signForm",
    component: signForm,
    meta: {
      title: "签到活动列表详情",
    },
  },
  //会员
  {
    path: "/member/thatperson",
    name: "thatperson",
    component: thatperson,
    meta: {
      title: "个人会员列表",
    },
  },
  {
    path: "/member/group",
    name: "group",
    component: group,
    meta: {
      title: "团体会员会员列表",
    },
  },

  {
    path: "/member/groupFom",
    name: "groupFom",
    component: groupFom,
    meta: {
      title: "团体会员会员详情",
    },
  },
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
  /**********************************************************************/
];

const router = new VueRouter({
  mode: "history",
  base: "/coach/admin/",
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 需要登录的页面
  const nextRoute = [
    "OrganizationUser",
    "IndividualUser",

    "CoachTrainManager",
    "CoachTrainForm",
    "RefereeTrainManager",
    "RefereeTrainForm",

    "CoachCertificationList",
    "CoachCertificationDetail",

    "RefereeCertificationList",
    "RefereeCertificationDetail",
    "RefereeExamList",
    "RefereeExamForm",

    "CoachTrainOrder",
    "RefereeTrainOrder",
    "RefereeRegisterOrder",
    "OrderDetail",
    "signlist",
    "signForm",
    "addchfc",
    "whitelist",
    "thatperson",
    "group",
  ];
  // const nextRoute = [];
  let isLogin = localStorage.getItem("ADMIN_LOGINTOKEN"); // 判断是否登录，本地存储有用户数据则视为已经登录
  // 未登录状态；当路由到 nextRoute 指定页时，跳转至 登录
  if (nextRoute.indexOf(to.name) >= 0) {
    // 检测是否登录的页面
    if (!isLogin) {
      // 如果未登录（本地存储无用户数据），并且要跳到登录页面
      if (from.name === "Login") {
        next("/login");
        return;
      }
      // 登录后，跳到到当前页面
      router.push({
        name: "Login",
      });
    }
  }

  // 已登录状态；当路由到 登录 时，跳转至 首页
  if (to.name === "Login") {
    if (isLogin) {
      next("/");
      return;
    }
  }
  next(); // 必须使用 next ,执行效果依赖 next 方法的调用参数
});
export default router;
